package day17;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

//测试Callble接口
public class TestCallable implements Callable<Integer> {
    //返回值：线程执行后，最终得到的结果
    public Integer call() throws Exception {
        int sum=0;
        for (int i=0;i<=100;i++){
            if (i%2==0){
                sum+=i;
                Thread.sleep(500);
                System.out.println("目前累加："+sum);
            }
        }
        return sum;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable call=new TestCallable();
        //Callable要实现线程类，必须借助于FutureTask类
        //底层是Runable接口的子类
        FutureTask<Integer> ft=new FutureTask(call);
        Thread t=new Thread(ft);
        t.start();
        //获取call()的返回值:属于线程阻塞状态
        //需要等待call()执行结束之后才会执行
        Integer sum=ft.get();
        System.out.println("计算的结果是："+sum);
    }

}
